<template>
    <yd-layout id="app">
        <navbar slot="navbar" :title="$route.meta.title" v-show="$route.meta.navShow"></navbar>
        <transition name="router-fade" mode="out-in">
            <keep-alive>
                <router-view v-if="$route.meta.keepAlive && isRouterAlive" class="top"></router-view>
            </keep-alive>
        </transition>
        <transition name="router-fade" mode="out-in">
            <router-view v-if="!isRouterAlive && !$route.meta.keepAlive" class="top"></router-view>
        </transition>
        <transition name="router-fade" mode="out-in">
            <router-view v-if="isRouterAlive && !$route.meta.keepAlive" class="top"></router-view>
        </transition>
        <tabbar slot="tabbar" v-show="$route.meta.tabShow"></tabbar>
    </yd-layout>
</template>

<script>
import navbar from './components/NavBar.vue'
import tabbar from './components/TabBar.vue'
import {mapGetters} from 'vuex'
import { setTimeout } from 'timers';

export default {
    data () {
        return {
            isRouterAlive: true
        }
    },
    components: {
        navbar, tabbar
    },
    computed: {
        ...mapGetters([
            'shopName',
            'shopDesc',
            'shopLogo',
			'statistics'
        ])
    },
    beforeMount () {
        this.getShopSetting();
    },
    provide () {
        return {
            reload: this.reload
        }
    },
    methods: {
        
        // 获取店铺配置 存入vuex
        getShopSetting () {
            this.$api.shopConfig().then(res => {
                this.$store.dispatch('shopConfig', res)
                if (this.$route.path === '/index') {
                    document.title = this.shopName
                }
                //百度统计
                if(res.statistics){
                    var script=document.createElement("script");
                    script.innerHTML = res.statistics;
                    document.getElementsByTagName("body")[0].appendChild(script);
                }
            });
        },
        reload () {
            this.isRouterAlive = false
            this.$nextTick(() => {
                this.isRouterAlive = true
            })
        },
        // 
        testPlat(){
            // alert(1)
            var u = navigator.userAgent;
            if(u.indexOf('Android') > -1 || u.indexOf('Adr') > -1){ //android终端
                let token  = window.androidStub.getUserToken()
                localStorage.setItem('user_token',token);
                // alert(token)/
                var location =  window.androidStub.getLocation()
                setTimeout((res=>{
                    let a = location.split(",")
                    this.$store.state.latitude = a[0]
                    this.$store.state.longitude = a[1]
                }),0)

                // return 0;
            }else if(!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)){ //ios终端
            //  window.webkit.messageHandlers.getLocation.postMessage();/
                    //  !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
                // let token = window.iOSToken;
                // localStorage.setItem('user_token',token); 
                // window.webkit.messageHandlers.getLocation.postMessage();
                // // window.webkit.messageHandlers.getBusinessLink.postMessage(val)
                // // window.webkit.messageHandlers.getBusinessLink.postMessage(val)
                // setTimeout((res=>{
                //     let a = location.split(",")
                //     this.$store.state.latitude = a[0]
                //     this.$store.state.longitude = a[1]
                // }),0)
                // alert(1)
                // console.log(11)
                // return 1;
                // let token  = window.androidStub.getToken()
                // localStorage.setItem("token",token);
                // var location =  window.androidStub.getLocation() 
                // setTimeout((res=>{
                //     let a = location.split(",")
                //         this.$store.state.latitude = a[0]
                //         this.$store.state.longitude = a[1]
                // }),0)
            }
        },
        getLocation(data){
            let a = data.split(",")
            this.$store.state.latitude = a[0]
            this.$store.state.longitude = a[1]
        },
        getUserToken(data){
            localStorage.setItem('user_token',data); 
        }
    },
    created(){ 
        // console.log(window.webkit)
        // console.log((window),"调用window")
        this.testPlat()
        this.getShopSetting()
        // window.getLocation = this.getLocation
        // window.getLocation();
    },
    mounted(){
        this.testPlat()
         window['getLocation'] = data=>{
            this.getLocation(data)
        }
        window['getUserToken'] = data=>{
            this.getUserToken(data) 
        }
        window['addFriend'] = data=>{
            this.addFriend(data) 
        }
        // let token = JSON.parse(JSON.stringify(window.iOSToken));
        // localStorage.setItem('user_token','00a79a21bbbbb6748f3a32fab8c37596');
        // let iOSInfo = JSON.parse(JSON.stringify(window.iOSInfo))
    },
    watch: {
        '$route': {
            handler () {
                document.title = this.$route.path === '/index' ? this.shopName : this.$route.meta.title
            }
        }
    }
}
</script>
<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  background-color: #F9F9F9;
}
#scrollView{
    margin-top: 0;
}
.router-fade-enter-active, .router-fade-leave-active {
    transition: opacity .3s;
}
.router-fade-enter, .router-fade-leave-active {
    opacity: 0;
}
</style>
